package com.kedou.sdk.ui.assist;

import android.content.Context;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.view.View;
import android.widget.TextView;

import com.kedou.sdk.utils.ResUtils;

public class LoginTypeSwitchWatcher implements View.OnClickListener {

    /**
     * 登录切换方式为手机号登录模式
     */
    public static final int SWITCH_MODE_WHICH_CELLPHONE = 0;

    /**
     * 登录切换方式为帐号登录模式
     */
    public static final int SWITCH_MODE_WHICH_ACCOUNT = 1;

//    private final TextView mMasterSwitchView;
    private final TextView mSlaveSwitchView;
    private final SpannableString[] mSwitchText = new SpannableString[2];

    private OnLoginTypeSwitchChangeListener mSwitchChangeListener;

    private int mCurrentLoginType;

    public LoginTypeSwitchWatcher( TextView slave) {
//        this.mMasterSwitchView = master;
        this.mSlaveSwitchView = slave;

        if (this.mSlaveSwitchView != null) {
            final Context context = this.mSlaveSwitchView.getContext();
            SpannableString content = new SpannableString(ResUtils.getString(context, "hnssn_login_phone_login"));
            content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
            SpannableString content2 = new SpannableString(ResUtils.getString(context, "hnssn_login_switch_account"));
            content2.setSpan(new UnderlineSpan(), 0, content2.length(), 0);
            this.mSwitchText[0] =content;
            this.mSwitchText[1] = content2;
            this.mSlaveSwitchView.setOnClickListener(this);
        }
    }

    public OnLoginTypeSwitchChangeListener getOnLoginTypeSwitchChangeListener() {
        return mSwitchChangeListener;
    }

    public void setOnLoginTypeSwitchChangeListener(OnLoginTypeSwitchChangeListener listener) {
        this.mSwitchChangeListener = listener;
    }

    private final void setCurrentLoginType(int loginType, boolean isNotifyListener) {
        this.mCurrentLoginType = loginType;
        if (SWITCH_MODE_WHICH_CELLPHONE == loginType) {

            this.mSlaveSwitchView.setText(mSwitchText[SWITCH_MODE_WHICH_ACCOUNT]);
            this.mSlaveSwitchView.setTag(mSwitchText[SWITCH_MODE_WHICH_CELLPHONE]);
        } else {

            this.mSlaveSwitchView.setText(mSwitchText[SWITCH_MODE_WHICH_CELLPHONE]);
            this.mSlaveSwitchView.setTag(mSwitchText[SWITCH_MODE_WHICH_ACCOUNT]);
        }

        if (mSwitchChangeListener != null && isNotifyListener) {
            mSwitchChangeListener.OnLoginTypeSwitchChanged(mSlaveSwitchView, loginType);
        }
    }

    public final void setCurrentLoginType(int loginType) {
        setCurrentLoginType(loginType, false);
    }

    public final boolean isCurrentCellphoneLoginType() {
        return SWITCH_MODE_WHICH_CELLPHONE == this.mCurrentLoginType;
    }

    @Override
    public void onClick(View view) {
        if (isCurrentCellphoneLoginType()) {
            setCurrentLoginType(SWITCH_MODE_WHICH_ACCOUNT, true);
        } else {
            setCurrentLoginType(SWITCH_MODE_WHICH_CELLPHONE, true);
        }
    }

    public interface OnLoginTypeSwitchChangeListener {
        void OnLoginTypeSwitchChanged(View view, int which);
    }
}
